﻿@using KC.Web.Admin.Constants
@using KC.MVC.Core.Constants
@using Microsoft.AspNetCore.Authorization
@using Microsoft.AspNetCore.Mvc.Rendering
@inject IAuthorizationService AuthorizationService

@{
    ViewData["Title"] = "系统日志";
    Layout = "~/Views/Shared/_ListLayout.cshtml";
    @*系统日志管理-删除系统日志*@
    var delinkbutton = false;
    @if ((await AuthorizationService.AuthorizeAsync(User, "1BF23319-EBEF-4E31-845E-8F1A1EC87529")).Succeeded)
    {
        delinkbutton = true;
    }
}

<div id="toolbar">
    <div>
        @*系统日志管理-删除系统日志*@
        @if ((await AuthorizationService.AuthorizeAsync(User, "1BF23319-EBEF-4E31-845E-8F1A1EC87529")).Succeeded)
        {
            <a href="javascript:void(0)" class="easyui-linkbutton" iconcls="fa fa-plus" onclick="deleteData(0)">清空日志</a>
        }
        <a href="javascript:void(0);" class="easyui-linkbutton" onclick="openForm()" iconcls="fa fa-pencil">查看详细错误</a>

        @*<a href="javascript:void(0);" class="easyui-linkbutton" onclick="deleteData()" data-options="iconCls:'fa fa-ban'" style="margin-left: 10px">清空日志</a>
        }*@
    </div>

    <div>
        <label>日志信息来源：</label> @Html.DropDownList("tableType", (IEnumerable<SelectListItem>)ViewBag.tableType, new { @id = "tableType", @class = "easyui-combobox", @style = "width:160px;height:26px", @DropDownStyle = "DropDownList" })
        <label style="margin-left: 10px">Log级别：</label>
        <select id="logLevel" class="easyui-combobox" style="width: 120px">
            <option value="全部">全部</option>
            <option value="Trace">Trace</option>
            <option value="Debug">Debug</option>
            <option value="Info">Info</option>
            <option value="Warn">Warn</option>
            <option value="Error">Error</option>
            <option value="Fatal">Fatal</option>
        </select>
        <a href="javascript:void(0);" class="easyui-linkbutton" onclick="searchData()" data-options="iconCls:'fa fa-refresh'" style="margin-left: 10px">查询</a>

    </div>
</div>
<table id="datagrid"></table>
<div align="justify"></div>
<div class="easyui-dialog" closed="true" id="dlg" title="详细错误" style="width:80%;height:400px;max-width:900px;padding:10px" data-options="iconCls:'icon-save'">
    <h2 id="messageTitle">消息内容</h2>
    <p id="messageContent">
    </p>
</div>
@section scripts
    {
    <script type="text/javascript">
        var delinkbutton = '@delinkbutton' === 'True';
        $(function() {
            InitTreeDataGrid();
            $('#tableType').combobox({
                editable: false
            });
            $('#logLevel').combobox({
                editable: false
            })
        });

        var loadDataUrl = '@Url.Action(ActionName.NLog.GetNLogJson, ControllerName.NLog)';
        var deteleDataUrl = '@Url.Action(ActionName.NLog.DeleteNLog, ControllerName.NLog)';
        var tableType;
        function InitTreeDataGrid() {
            var $dataGrid = $('#datagrid').datagrid({
                    url: loadDataUrl,
                    method: 'get',
                    idField: '',
                    striped: true,
                    fitColumns: true,
                    rownumbers: true,
                    singleSelect: true,
                    nowrap: false,
                    fit: true,
                    pagination: true,
                    showFooter: false,
                    toolbar: "#toolbar",
                    //toolbar: [{ text: document.getElementById('toolbar') }],
                    columns: [[
                            { field: 'LoggerName', title: '名称', width: 120, align: 'center' },
                            { field: 'Level', title: 'Log级别', width: 50, align: 'center' },
                            { field: 'LogTimeStamp', title: '时间', width: 80, align: 'center', sortable: true },
                            { field: 'MachineName', title: '远程服务器', width: 80, align: 'center' },
                            //{ field: 'StackTrace', title: '堆栈信息', width: 50, align: 'center' },
                            { field: 'df', title: '操作', width: 150, align: 'center',
                                formatter: function(value, row, index) {
                                    if (delinkbutton) {
                                        return '<a href="javascript:void(0);"  style="margin-right: 10px"onclick="deleteData(\'' + row.RowKey + '\')" class="delete">删除</a>'
                                            //+ '<a href="javascript:void(0);" onclick="openForm(\'' + row.RowKey + '\')" class="exception">查看详细错误</a>';

                                    } else {
                                        //return '<a href="javascript:void(0);" onclick="openForm(\'' + row.RowKey + '\')" class="exception">查看详细错误</a>';

                                    }
                                }
                            }
                        ]
                    ],
                    onBeforeLoad: function(param) {

                    },
                    onLoadSuccess: function(data) {
                        $('.exception').linkbutton({
                            iconCls: 'fa fa-refresh'
                        });
                        $('.delete').linkbutton({
                            iconCls: 'fa fa-trash'
                        });
                        tableType = $('#tableType').combobox('getValue');
                    },
                    onLoadError: function() {
                    }
                }
            );

        }

        function openForm() {
            //$("#datagrid").datagrid("selectRow", RowKey);
            var row = $('#datagrid').datagrid('getSelected');
            if (row == null) {
                $.messager.showErrorCenter('系统提示', "请选中要查看的行", 1000);
            }
            else {
                $('#messageContent').html(row.Message != null ? row.Message : "无消息");
                $('#dlg').dialog('open');
            }
            //    $('#exceptionContent').html(row.Exception != null ? row.Exception : "无错误内容");

        }


        function deleteData(value) {
            var title = value == 0 ?
                '是否要清空"' + $('#tableType').combobox('getText') + '"的全部日志？（PS:清空日志所需一定时间,请耐心等待！）'
                : '是否要删除该条记录？';
            var keyValue;
            if (value != '') {
                keyValue = value;
            } else {
                keyValue = "";
            }

            $.messager.confirm('系统提示', title, function(r) {
                if (r) {
                    $.easyui.loading({ msg: '正在保存数据，请稍等...' });
                    $.post(deteleDataUrl,
                        {
                            rowKey: keyValue,
                            tableType: keyValue == "" ? $('#tableType').combobox('getValue') : tableType
                        },
                        function(data) {
                            if (data.success) {
                                if (data.Result) {
                                    $('#datagrid').datagrid('clearSelections');
                                    $('#datagrid').datagrid('reload');
                                    $.messager.showInfoCenter('系统提示', '删除数据成功。', 1000);
                                } else {
                                    $.messager.showErrorCenter('错误消息', '删除数据失败。', 1000);
                                }
                            } else {
                                $.data.showErrorCenter('错误消息', data.message, 1000);
                            }
                            $.easyui.loaded();
                        }, 'json');
                }
            });
        }

        function searchData() {
            $('#datagrid').datagrid('unselectAll');
            $('#datagrid').datagrid('load', {
                "logLevel": $('#logLevel').combobox('getValue'),
                "tableType": $("#tableType").combobox('getValue')
            });
        }

    </script>
}


